package gw.reportbuilder.filter;

import gw.reportbuilder.column.Column;

public class Filter {

    private final Column column;
    private Operator operator;
    private String[] args;
    

    public Filter(Column column) {

        this.column = column;
        validate();
    }
    
    public void setArguments(String... args) {
        
        this.args = args;
    }
    
    public String getSQLClause() {
        
        return this.operator.getSQLClause(column, args);
    }
    
    public void setOperator(Operator operator) {
        
        this.operator = operator;
    }

    public Operator getOperator() {

        return operator;
    }

    public Column getColumn() {
        return column;
    }

    private void validate() {

        if (column == null) {
            throw new IllegalArgumentException("O filtro deve possuir uma coluna.");
        }
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Filter other = (Filter) obj;
        if (this.column != other.column && (this.column == null || !this.column.equals(other.column))) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 5;
        hash = 31 * hash + (this.column != null ? this.column.hashCode() : 0);
        return hash;
    }

    @Override
    public String toString() {

        return "filter[" + column.getName() + " as " + column.getLabel() + "]";
    }
}
